package keeto.com.vn.classified.adapter;

import java.util.ArrayList;

import keeto.com.vn.classified.network.entity.ListItemEntity;
import keeto.com.vn.classified.ui.IADetailProductActivity;
import keeto.com.vn.classified.ui.R;
import keeto.com.vn.classified.utils.StringUtilities;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.androidquery.AQuery;

public class HomeProductAdapter extends BaseAdapter{

	private Context context;
	private ArrayList<ListItemEntity> List;
	private LayoutInflater  inflater;
	private AQuery mQuery;
	

	public HomeProductAdapter(Context context, ArrayList<ListItemEntity> list, AQuery query) {
		super();
		this.context = context;
		this.List = list;
		this.inflater = LayoutInflater.from(context);
		this.mQuery = query;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return List.size();
	}
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return List.get(position);
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public class ViewHolder{
		private ImageView img_product;
		private TextView tv_discount;
		private TextView tv_product_name;
		private RatingBar ratingBar_product;
		private TextView tv_price_product;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;		
		if (convertView== null){
			convertView = inflater.inflate(R.layout.iahome_product_layout,parent, false);
			holder = new ViewHolder();
			holder.img_product = (ImageView) convertView.findViewById(R.id.imv_home_product);
			holder.tv_discount = (TextView)convertView.findViewById(R.id.tv_home_discount);
			holder.tv_product_name = (TextView)convertView.findViewById(R.id.tv_home_product_name);
			holder.ratingBar_product = (RatingBar)convertView.findViewById(R.id.ratingbar_home_product);
			holder.tv_price_product = (TextView)convertView.findViewById(R.id.tv_home_price);
			holder.tv_discount.setVisibility(View.GONE);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}

		
		final ListItemEntity product = List.get(position);
		if (product.getCoverPicture().size() > 0 && !TextUtils.isEmpty(product.getCoverPicture().get(0))) {
			String url = product.getCoverPicture().get(0);
			mQuery.id(holder.img_product).image(url, true, true, 0, 0, null, 0, 1.0f);
		} else {
			mQuery.id(holder.img_product).image(R.drawable.no_image);

		}

		/*
		 * set data to UI
		 */
		String productname = product.getTitle();
		if (productname.length() > 27){
			holder.tv_product_name.setText( product.getTitle().substring(0, 24 )+ "...");

		}else{
			holder.tv_product_name.setText(product.getTitle());
		}
		/*
		 * price
		 */
		String price = StringUtilities.convertPrice(product.getPrice());

		if(product.getDiscount() > 0){
			holder.tv_discount.setText((int)(product.getDiscount()) +"%");
			holder.tv_discount.setVisibility(View.VISIBLE);
			String new_price = StringUtilities.convertNewPrice(product.getPrice(), product.getDiscount());
			if (!TextUtils.isEmpty(new_price) && !new_price.equals("0")) {
				holder.tv_price_product.setText(new_price + " VNĐ");
				holder.tv_price_product.setVisibility(View.VISIBLE);
			} else {
				holder.tv_price_product.setVisibility(View.INVISIBLE);
			}

		}else{
			holder.tv_discount.setVisibility(View.GONE);
			if (!TextUtils.isEmpty(price) && !price.equals("0")) {
				holder.tv_price_product.setVisibility(View.VISIBLE);
				holder.tv_price_product.setText(price + " VNĐ");
			} else {
				String additionPrice = product.getAdditionPrice();
				if(additionPrice != null && additionPrice.length() > 0){
					holder.tv_price_product.setText(Html.fromHtml(additionPrice));
					holder.tv_price_product.setVisibility(View.VISIBLE);
				}else{
					holder.tv_price_product.setVisibility(View.INVISIBLE);
				}
			}
		}

		/*
		 * ratingbar:
		 */
		float rating = product.getRating();
		holder.ratingBar_product.setRating(rating);

		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				IADetailProductActivity.actionLaunchProduct(context, product);
			}
		});

		return convertView;
	}

}
